#include "../head.h"

struct messageDate
{
    char msg[1024];
    struct sockaddr_in recvAddr;
};

int main()
{
    // 1.创建套接字
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    // 2.创建结构体
    struct sockaddr_in client_addr;
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(6153);
    client_addr.sin_addr.s_addr = inet_addr("192.168.47.198");
    // 3.发送数据
    while (1)
    {
        struct messageDate msg;
        msg.sockaddr_in.sin_family = AF_INET;
        int port;
        printf("请输入端口号：");
        scanf("%d\n", port);
        if (port < 0 || port > 65535)
        {
            printf("端口号错误！\n");
            return -1;
        }
        msg.sockaddr_in.sin_port = htons(port);
        char ip[32];
        printf("请输入ip地址:");
        scanf("%s\n", ip);
        msg.sockaddr_in.sin_addr.s_addr = inet_addr(ip);
        char buf[1024];
        printf("请输入要发送的消息：");
        scanf("%s\n", msg.msg);
        // 3.发送消息
        sendto(sockfd, msg.msg, strlen(msg.msg), 0, (struct sockaddr *)&msg.sockaddr_in, sizeof(msg.sockaddr_in));
    }

    return 0;
}